package cn.demo.d01;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Demo06 {

    public static void main(String[] args) {
        // test36();
        // System.out.println("--------------------------------");
        // test37();
        // test38();
        // System.out.println("--------------------------------");
        // test39();
        // System.out.println("--------------------------------");
        test40();
        System.out.println("--------------------------------");
    }

    /**
     //判断 101 ~ 200 之间有多少个素数，并打印所有素数

     //思路一： 2 ~ 99
     //定义变量i ，赋值100
     //判断i是否为质数

     //定义一个变量用来统计有多少个质数
     * */
    private static void test36(){
        int count = 0;
        for (int i = 101; i <=200 ; i++) {
            boolean flag = false;
            for (int j = 2; j < i ; j++) {
                if(i%j==0){
                    flag = true;
                    break;
                }
            }
            if (!flag){
                System.out.println(i+"是质数");
                count++;
            }
        }
        System.out.println("一共有"+count+"个质数");
    }

    /**
     需求： 定义方法实现随机产生一个5位的验证码
     验证码格式：
         长度为5
         前四位是大写字母或者小写字母
         最后一位是数字
     */
    private static void test37(){
        char[] chs = new char[52];
        for (int i = 0; i <26; i++) {
            chs[i] = (char)(i+65);
        }
        int count = 97;
        for (int i = chs.length/2; i < chs.length; i++) {
            chs[i] = (char)count++;
        }
        System.out.println(Arrays.toString(chs));

        //定义一个字符串类型的变量，用来记录最终的结果
        StringBuilder result = new StringBuilder();
        Random r = new Random();
        for (int i = 0; i < 4; i++) {
            result.append(chs[r.nextInt(chs.length)]);
        }
        result.append(r.nextInt(10));
        System.out.println(result);
    }

    private static void test38(){
        char[] chs = new char[52];
        int x = 0;
        for (char i = 'a'; i <='z'; i++) {
            chs[x] = i;
            x++;
        }
        for (char i = 'A'; i <= 'Z'; i++) {
            chs[x] = i;
            x++;
        }
        System.out.println(Arrays.toString(chs));
    }

    /*
        // 在唱歌比赛中，有6名评委给选手打分，分数范围是[0 - 100]之间的整数。
        // 选手的最后得分为：去掉最高分、最低分后的4个评委的平均分，请完成上述过程并计算出选手的得分。
    * */
    private static void test39(){
        System.out.println("一个几个评为");
        Scanner scanner = new Scanner(System.in);
        int pwCount = scanner.nextInt();
        int[] scores = getScores(pwCount, scanner);
        int totalScore = getTotalScore(scores);
        int maxScore = getMaxScore(scores);
        int minScore = getMinScore(scores);
        double avgScore = (totalScore - maxScore - minScore) / (scores.length - 2.0);
        System.out.println("选手的最后得分为：" + avgScore);
    }

    private static int getTotalScore(int[] scores) {
        int sum = 0;
        for (int i = 0; i < scores.length; i++) {
            sum += scores[i];
        }
        return sum;
    }

    private static int getMaxScore(int[] scores) {
        int max = scores[0];
        for (int i = 1; i < scores.length; i++) {
            if (scores[i] > max) {
                max = scores[i];
            }
        }
        return max;
    }

    private static int getMinScore(int[] scores) {
        int min = scores[0];
        for (int i = 1; i < scores.length; i++) {
            if (scores[i] < min) {
                min = scores[i];
            }
        }
        return min;
    }

    private static int[] getScores(int pwCount, Scanner scanner) {
        int[] scores = new int[pwCount];
        for (int i = 0; i < pwCount; ) {
            System.out.println("请输入第"+(i+1)+"个评为分数:");
            int score = scanner.nextInt();
            if (score >= 0 && score <= 100) {
                scores[i] = score;
                i++;
            }else{
                System.out.println("输入的分数不在[0-100]范围内，请重新输入");
            }
        }
        return scores;
    }

    /*
        某系统的数字密码（大于0）。比如1983，采用加密方式进行传输，
        规则如下：
            每位数加上5
            再对10求余，
            最后将所有数字反转，
            得到一串新数。
    */
    private static void test40(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入原始密码:");
        String oldPwd = sc.next();
        int[]  oldPwdArr = new int[oldPwd.length()];
        // for (int i = 0; i < oldPwdArr.length; i++) {
        //     oldPwdArr[i] = oldPwd.charAt(i) - '0';
        // }
        for (int i = 0; i < oldPwdArr.length; i++) {
            oldPwdArr[i] = oldPwd.charAt(i) - '0';
            oldPwdArr[i] = (oldPwdArr[i]+5)%10;
        }
        System.out.println(Arrays.toString(oldPwdArr));
        for (int i = 0,j=oldPwdArr.length-1; i < j; i++,j--) {
            int temp = oldPwdArr[i];
            oldPwdArr[i] = oldPwdArr[j];
            oldPwdArr[j] = temp;
        }
        int result = 0;
        for (int i = 0; i < oldPwdArr.length; i++) {
            result = result*10+oldPwdArr[i];
        }
        System.out.println(Arrays.toString(oldPwdArr));
        System.out.println(result);
    }
}
